package com.croofr.dao.service.implementation.database.dialect;

import com.croofr.dao.service.Dialect;
import com.croofr.dao.service.KeyGenerator;
import com.croofr.dao.service.PersistentError;
import com.croofr.dao.service.ValueTransformer;
import com.croofr.dao.service.implementation.database.generator.OracleKeyGenerator;
import com.croofr.dao.service.implementation.database.transformer.OracleValueTransformer;

import static com.croofr.dao.service.PersistentError.GENERIC_ERROR;
import static com.croofr.dao.service.PersistentError.UNIQUE_UNIT_NAME;
import static com.croofr.global.constants.URLConstants.PORT_DELIMITER;

/**
 * User: bednar
 * Date: Mar 22, 2010
 * Time: 6:34:44 PM
 */
public class OracleDialect implements Dialect
{
    @Override
    public String getDriverClassName()
    {
        return "oracle.jdbc.OracleDriver";
    }

    @Override
    public String getConnectionUrl(String... args)
    {
        StringBuffer url = new StringBuffer();

        url.append("jdbc:oracle:thin:@");
        url.append(args[0]);
        url.append(PORT_DELIMITER);
        url.append(args[1]);
        url.append(PORT_DELIMITER);
        url.append(args[2]);

        return url.toString();
    }

    @Override
    public String execeptionCode(Exception sqlException)
    {

        PersistentError persistentError = GENERIC_ERROR;

        String errorCode = sqlException.getLocalizedMessage().split(":")[0];
        if ("ORA-00955".equals(errorCode))
        {
            persistentError = UNIQUE_UNIT_NAME;
        }

        return persistentError.toString();
    }

    @Override
    public KeyGenerator getKeyGenerator()
    {
        return new OracleKeyGenerator("croofrKey");
    }

    @Override
    public ValueTransformer getValueTransformer()
    {
        return new OracleValueTransformer();
    }
}
